package com.zjw.zy.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.zjw.zy.utils.taskpool.BackGroundExecutor;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/9 9:08
 * <p>
 * 手势recyclerView
 */
public class GestureRecyclerView extends RecyclerView {

    @Nullable
    private OnGestureListener mListener;

    private final GestureDetector mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (distanceX > 30 && Math.abs(distanceY) < 10) {
                GestureViewHolder<?> viewHolder = findGestureViewHolder(e1.getX(), e1.getY());

                if (viewHolder != null) {
                    viewHolder.onLeftSlide(GestureRecyclerView.this, e2);

                    if (mListener != null) {
                        mListener.onLeftSlide(viewHolder, e2);
                    }
                }

                return true;
            }


            if (distanceX < 0 && Math.abs(distanceX) > 30 && Math.abs(distanceY) < 10) {
                GestureViewHolder<?> viewHolder = findGestureViewHolder(e1.getX(), e1.getY());

                if (viewHolder != null) {
                    viewHolder.onRightSlide(GestureRecyclerView.this, e2);

                    if (mListener != null) {
                        mListener.onRightSlide(viewHolder, e2);
                    }
                }

                return true;
            }


            if (distanceY < 0 && Math.abs(distanceX) < 10 && Math.abs(distanceY) > 30) {
                GestureViewHolder<?> viewHolder = findGestureViewHolder(e1.getX(), e1.getY());

                if (viewHolder != null) {
                    viewHolder.onDownSlide(GestureRecyclerView.this, e2);

                    if (mListener != null) {
                        mListener.onDownSlide(viewHolder, e2);
                    }
                }

                return true;
            }


            if (distanceY > 30 && Math.abs(distanceX) < 10) {
                GestureViewHolder<?> viewHolder = findGestureViewHolder(e1.getX(), e1.getY());

                if (viewHolder != null) {
                    viewHolder.onUpSlide(GestureRecyclerView.this, e2);

                    if (mListener != null) {
                        mListener.onUpSlide(viewHolder, e2);
                    }
                }
                return true;
            }
            return super.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            GestureViewHolder<?> viewHolder = findGestureViewHolder(e.getX(), e.getY());

            if (viewHolder != null) {
                viewHolder.onClick(GestureRecyclerView.this, e);

                if (mListener != null) {
                    mListener.onClick(viewHolder, e);
                }

                BaseRecyclerAdapter.ItemListener<BaseViewHolder<?>> adapterListener = getAdapterListener();
                if (adapterListener != null) {
                    adapterListener.onItemClick(viewHolder, viewHolder.itemView);
                }
            }

            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            GestureViewHolder<?> viewHolder = findGestureViewHolder(e.getX(), e.getY());

            if (viewHolder != null) {
                viewHolder.onLongPress(GestureRecyclerView.this, e);

                if (mListener != null) {
                    mListener.onLongPress(viewHolder, e);
                }

                BaseRecyclerAdapter.ItemListener<BaseViewHolder<?>> adapterListener = getAdapterListener();
                if (adapterListener != null) {
                    adapterListener.onItemLongClick(viewHolder, viewHolder.itemView);
                }
            }

        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            GestureViewHolder<?> viewHolder = findGestureViewHolder(e.getX(), e.getY());

            if (viewHolder != null) {
                viewHolder.onDoubleClick(GestureRecyclerView.this, e);

                if (mListener != null) {
                    mListener.onDoubleClick(viewHolder, e);
                }

                BaseRecyclerAdapter.ItemListener<BaseViewHolder<?>> adapterListener = getAdapterListener();
                if (adapterListener != null) {
                    adapterListener.onItemDoubleClick(viewHolder, viewHolder.itemView);
                }
            }

            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return super.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public boolean onDown(MotionEvent e) {
            GestureViewHolder<?> viewHolder = findGestureViewHolder(e.getX(), e.getY());

            if (viewHolder != null) {
                viewHolder.onDown(GestureRecyclerView.this, e);

                if (mListener != null) {
                    mListener.onDown(viewHolder, e);
                }
            }

            return true;
        }
    });


    public GestureRecyclerView(@NonNull Context context) {
        super(context);
    }

    public GestureRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public GestureRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        return true;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        //ignore return
        mGestureDetector.onTouchEvent(e);
        return super.onTouchEvent(e);
    }

    @Nullable
    private GestureViewHolder<?> findGestureViewHolder(float x, float y) {
        View child = findChildViewUnder(x, y);

        ViewHolder viewHolder = null;
        if (child != null) {
            viewHolder = findContainingViewHolder(child);
        }

        if (viewHolder instanceof GestureViewHolder) {
            return ((GestureViewHolder<?>) viewHolder);
        }

        return null;
    }

    public void bindGestureListener(@Nullable OnGestureListener listener) {
        mListener = listener;
    }

    protected BaseRecyclerAdapter.ItemListener<BaseViewHolder<?>> getAdapterListener() {
        return ((BaseRecyclerAdapter) getAdapter()).mItemListener;
    }


    public interface OnGestureListener {

        default void onLeftSlide(@NonNull GestureViewHolder<?> holder, @NonNull MotionEvent event) {
        }

        default void onRightSlide(@NonNull GestureViewHolder<?> holder, @NonNull MotionEvent event) {
        }

        default void onUpSlide(@NonNull GestureViewHolder<?> holder, @NonNull MotionEvent event) {
        }

        default void onDownSlide(@NonNull GestureViewHolder<?> holder, @NonNull MotionEvent event) {
        }

        default void onClick(@NonNull GestureViewHolder<?> holder, @NonNull MotionEvent event) {

        }

        default void onLongPress(@NonNull GestureViewHolder<?> holder, @NonNull MotionEvent event) {

        }

        default void onDoubleClick(@NonNull ViewHolder holder, @NonNull MotionEvent event) {

        }

        default void onDown(@NonNull ViewHolder holder, @NonNull MotionEvent event) {

        }
    }
}
